package com.schoolhostptail.controller;

import com.schoolhostptail.entity.Admin;
import com.schoolhostptail.entity.Doctor;
import com.schoolhostptail.service.IAdminService;
import com.schoolhostptail.service.IDoctorService;
import com.schoolhostptail.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登录
 */
@Controller
@RequestMapping("/api/login")
public class LoginController {

    @Autowired
    private IAdminService iAdminService;

    @Autowired
    private IDoctorService iDoctorService;

    /**
     * 管理员登录
     * @return
     */
    @RequestMapping("/author")
    @ResponseBody
    public Result author(HttpServletRequest request, HttpSession session) {
        String type=request.getParameter("type");
        String account=request.getParameter("account");
        String pwd=request.getParameter("pwd");
        if(type.equals("1")){//管理员登录
            Admin admin=this.iAdminService.login(account,pwd);
            if(admin==null){
                return Result.reError("账户密码错误");
            }
            //保存登录数据
            session.setAttribute("admin",admin);
        }else{//医生登录
            Doctor doctor=this.iDoctorService.login(account,pwd);
            if(doctor==null){
                return Result.reError("账户密码错误");
            }
            //保存登录数据
            session.setAttribute("doctor",doctor);
        }

        return Result.reOk("登录成功");
    }

    /**
     * 获取当前登录的用户信息
     * @return
     */
    @RequestMapping("/getAccountInfo")
    @ResponseBody
    public Result getAccountInfo(HttpServletRequest request, HttpSession session) {
        String type=request.getParameter("type");
        if(type.equals("1")){
            Admin admin=(Admin)session.getAttribute("admin");
            if(admin==null){
                return Result.reError("登录信息已过期");
            }
            session.removeAttribute("doctor");
            return Result.reOk("查询成功",admin);
        }

        if(type.equals("2")){
            Doctor doctor=(Doctor)session.getAttribute("doctor");
            if(doctor==null){
                return Result.reError("登录信息已过期");
            }
            session.removeAttribute("admin");
            return Result.reOk("查询成功",doctor);
        }
        return Result.reError("登录信息已过期");
    }


    /**
     * 修改的用户信息
     * @return
     */
    @RequestMapping("/updateAccount")
    @ResponseBody
    public Result updateAccount(HttpServletRequest request, HttpSession session) {
        Admin admin=(Admin)session.getAttribute("admin");
        if(admin==null){
            return Result.reError("登录信息已过期");
        }
        String username= request.getParameter("username");
        String nickname=request.getParameter("nickname");
        String pwd=request.getParameter("pwd");
        admin.setUsername(username);
        admin.setNickname(nickname);
        admin.setPwd(pwd);
        this.iAdminService.updateById(admin);
        return Result.reOk("修改成功",admin);
    }



}
